﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Battle;
using DG.Tweening;

namespace Battle
{
    public class EnemyMoveBase : MonoBehaviour
    {
        public Vector3 CurDir { get; protected set; }
        public MyNumber MoveSpeed { get; protected set; }
        public bool IsBeRepeled { get; protected set; } = false;

        public void Init( Vector3 target, float speed)
        {
            CurDir = (target - transform.position).normalized;
            MoveSpeed = new MyNumber(speed);
        }

        protected virtual void Update()
        {
            if (!IsBeRepeled)
                Move(Time.deltaTime);
        }

        protected virtual void Move(float deltaTime)
        {
            transform.position += deltaTime * CurDir * MoveSpeed.Value;
        }

        public virtual void OnRepel(float ownNum, float repelNum, Vector3 dir)
        {
            var repelResult = BattleTools.GetRepelResult(repelNum, ownNum);

            Vector3 targetPos = transform.position + dir * repelResult.dis;

            //越界处理
            if (Mathf.Abs(targetPos.x) > GameMgr.ScreenSize.x)
                targetPos.x = (targetPos.x > 0 ? 1 : -1) * GameMgr.ScreenSize.x;

            transform.DOKill();
            transform.DOMove(targetPos, repelResult.time).OnComplete(OnRepelOver);
        }

        protected void OnRepelOver()
        {
            IsBeRepeled = false;
        }

        private void OnDestroy()
        {
            transform.DOKill();
        }
    }
}
